Author: Hui Ma, Yiming Yang, Rimte Rocher
Date: 2022-02-24
Notebook Source: batch_correction.ipynb
import pegasus as pg
In this tutorial, we'll use a gene-count matrix dataset on human bone marrow from 8 donors, and show how to use the batch correction methods in Pegasus to tackle the batch effects in the data.
The dataset is stored at https://storage.googleapis.com/terra-featured-workspaces/Cumulus/MantonBM_nonmix_subset.zarr.zip. You can also use gsutil to download it via its Google bucket URL (gs://terra-featured-workspaces/Cumulus/MantonBM_nonmix_subset.zarr.zip).
Now load the count matrix:
data = pg.read_input("MantonBM_nonmix_subset.zarr.zip")
data
2022-03-06 22:11:19,950 - pegasusio.readwrite - INFO - zarr file 'MantonBM_nonmix_subset.zarr.zip' is loaded. 2022-03-06 22:11:19,951 - pegasusio.readwrite - INFO - Function 'read_input' finished in 0.31s.
MultimodalData object with 1 UnimodalData: 'GRCh38-rna'
It currently binds to UnimodalData object GRCh38-rna
UnimodalData object with n_obs x n_vars = 48219 x 36601
Genome: GRCh38; Modality: rna
It contains 1 matrix: 'X'
It currently binds to matrix 'X' as X
obs: 'n_genes', 'Channel', 'gender'
var: 'featureid'
uns: 'genome', 'modality'
'Channel' is the batch key. Each batch is associated with one donor, so there are 8 batches in total.
First, preprocess the data. This includes filtration, selecting robust genes, and log-normalization:
pg.qc_metrics(data, min_genes=500, max_genes=6000, mito_prefix='MT-', percent_mito=10)
pg.filter_data(data)
pg.identify_robust_genes(data)
pg.log_norm(data)
2022-03-06 22:11:20,551 - pegasusio.qc_utils - INFO - After filtration, 35465 out of 48219 cell barcodes are kept in UnimodalData object GRCh38-rna. 2022-03-06 22:11:20,552 - pegasus.tools.preprocessing - INFO - Function 'filter_data' finished in 0.25s. 2022-03-06 22:11:21,230 - pegasus.tools.preprocessing - INFO - After filtration, 25653/36601 genes are kept. Among 25653 genes, 17516 genes are robust. 2022-03-06 22:11:21,231 - pegasus.tools.preprocessing - INFO - Function 'identify_robust_genes' finished in 0.68s. 2022-03-06 22:11:21,921 - pegasus.tools.preprocessing - INFO - Function 'log_norm' finished in 0.69s.
After quality-control, distribution of cells in all the 8 batches is:
data.obs['Channel'].value_counts()
MantonBM2_HiSeq_1 4935 MantonBM6_HiSeq_1 4665 MantonBM8_HiSeq_1 4511 MantonBM7_HiSeq_1 4452 MantonBM1_HiSeq_1 4415 MantonBM3_HiSeq_1 4225 MantonBM4_HiSeq_1 4172 MantonBM5_HiSeq_1 4090 Name: Channel, dtype: int64
We first perform downstream steps without considering batch effects. In this way, you can see where the batch effects exist, and moreover, we'll use this result as the baseline when comparing different batch correction methods.
data_baseline = data.copy()
pg.highly_variable_features(data_baseline)
2022-03-06 22:11:22,396 - pegasus.tools.hvf_selection - INFO - Function 'estimate_feature_statistics' finished in 0.10s. 2022-03-06 22:11:22,435 - pegasus.tools.hvf_selection - INFO - 2000 highly variable features have been selected. 2022-03-06 22:11:22,436 - pegasus.tools.hvf_selection - INFO - Function 'highly_variable_features' finished in 0.14s.
In this tutorial, the downstream steps consists of:
pg.pca(data_baseline)
pg.neighbors(data_baseline)
pg.louvain(data_baseline)
pg.umap(data_baseline)
pg.scatter(data_baseline, attrs=['louvain_labels', 'Channel'], basis='umap')
2022-03-06 22:11:29,514 - pegasus.tools.preprocessing - INFO - Function 'pca' finished in 7.07s.
2022-03-06 22:11:41,597 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 12.08s.
2022-03-06 22:11:43,067 - pegasus.tools.nearest_neighbors - INFO - Function 'calculate_affinity_matrix' finished in 1.47s.
2022-03-06 22:11:44,427 - pegasus.tools.graph_operations - INFO - Function 'construct_graph' finished in 1.30s.
2022-03-06 22:12:14,549 - pegasus.tools.clustering - INFO - Louvain clustering is done. Get 19 clusters.
2022-03-06 22:12:14,582 - pegasus.tools.clustering - INFO - Function 'louvain' finished in 31.51s.
2022-03-06 22:12:14,583 - pegasus.tools.nearest_neighbors - INFO - Found cached kNN results, no calculation is required.
2022-03-06 22:12:14,584 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 0.00s.
UMAP(min_dist=0.5, precomputed_knn=(array([[ 0, 30526, 27514, ..., 26911, 222, 25411],
[ 1, 29723, 2651, ..., 4946, 1177, 30829],
[ 2, 35262, 20170, ..., 2478, 34953, 19206],
...,
[35462, 6096, 30824, ..., 15433, 19601, 30227],
[35463, 34252, 34709, ..., 5765, 32603, 31084],
[35464, 5379, 35401, ..., 10528, 24551, 27891]]), array([[ 0. , 4.984169 , 5.4866176, ..., 5.8861804, 5.8992977,
5.901227 ],
[ 0. , 8.275161 , 8.679711 , ..., 9.440163 , 9.484828 ,
9.490481 ],
[ 0. , 4.793777 , 5.1250052, ..., 5.489494 , 5.4935 ,
5.4957047],
...,
[ 0. , 7.3680816, 7.414498 , ..., 8.2814455, 8.314083 ,
8.348549 ],
[ 0. , 8.759402 , 8.7638645, ..., 10.250344 , 10.255535 ,
10.261413 ],
[ 0. , 7.129679 , 7.265322 , ..., 8.02741 , 8.158878 ,
8.207317 ]], dtype=float32), <pegasus.tools.visualization.DummyNNDescent object at 0x7ffa1f059750>), random_state=0, verbose=True)
Sun Mar 6 22:12:14 2022 Construct fuzzy simplicial set
OMP: Info #273: omp_set_nested routine deprecated, please use omp_set_max_active_levels instead.
Sun Mar 6 22:12:17 2022 Construct embedding
Epochs completed: 0%| 0/200 [00:00]
Sun Mar 6 22:12:45 2022 Finished embedding 2022-03-06 22:12:45,218 - pegasus.tools.visualization - INFO - Function 'umap' finished in 30.64s.
Let's have a quick look at the UMAP plot above. If checking the cells in Louvain cluster 11 and 14 in the right-hand-side plot, you can see that most of them are from sample MantonBM3_HiSeq_1. This indicates strong batch effects.
Batch effect occurs when data samples are generated in different conditions, such as date, weather, lab setting, equipment, etc. Unless informed that all the samples were generated under the similar condition, people may suspect presumable batch effects if they see a visualization graph with samples kind-of isolated from each other.
In this tutorial, you'll see how to apply the batch correction methods in Pegasus to this dataset.
As a common step ahead, we need to re-select HVGs considering batch effects:
pg.highly_variable_features(data, batch='Channel')
2022-03-06 22:12:47,138 - pegasus.tools.hvf_selection - INFO - Function 'estimate_feature_statistics' finished in 0.32s. 2022-03-06 22:12:47,192 - pegasus.tools.hvf_selection - INFO - 2000 highly variable features have been selected. 2022-03-06 22:12:47,193 - pegasus.tools.hvf_selection - INFO - Function 'highly_variable_features' finished in 0.38s.
Harmony is a widely-used method for data integration. Pegasus uses harmony-pytorch package to perform Harmony batch correction.
Harmony works on PCA matrix. So we need to first calculate the original PCA matrix:
data_harmony = data.copy()
pg.pca(data_harmony)
2022-03-06 22:12:53,833 - pegasus.tools.preprocessing - INFO - Function 'pca' finished in 6.28s.
Now we are ready to run Harmony integration:
harmony_key = pg.run_harmony(data_harmony)
2022-03-06 22:12:54,619 - pegasus.tools.batch_correction - INFO - Start integration using Harmony. Initialization is completed. Completed 1 / 10 iteration(s). Completed 2 / 10 iteration(s). Completed 3 / 10 iteration(s). Completed 4 / 10 iteration(s). Completed 5 / 10 iteration(s). Completed 6 / 10 iteration(s). Completed 7 / 10 iteration(s). Completed 8 / 10 iteration(s). Reach convergence after 8 iteration(s). 2022-03-06 22:13:19,627 - pegasus.tools.batch_correction - INFO - Function 'run_harmony' finished in 25.79s.
When finished, the corrected PCA matrix is stored in data_harmony.obsm['X_pca_harmony'], and run_harmony returns the representation key 'pca_harmony' as variable harmony_key. In the downstream steps, you can set rep parameter to either harmony_key or 'pca_harmony' in Pegasus functions whenever applicable.
For details on parameters of run_harmony other than the default setting, please see here.
With the new corrected PCA matrix, we can perform kNN-graph-based clustering and calculate UMAP embeddings as follows:
pg.neighbors(data_harmony, rep=harmony_key)
pg.louvain(data_harmony, rep=harmony_key)
pg.umap(data_harmony, rep=harmony_key)
2022-03-06 22:13:30,511 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 10.88s.
2022-03-06 22:13:32,018 - pegasus.tools.nearest_neighbors - INFO - Function 'calculate_affinity_matrix' finished in 1.51s.
2022-03-06 22:13:33,540 - pegasus.tools.graph_operations - INFO - Function 'construct_graph' finished in 1.52s.
2022-03-06 22:13:56,049 - pegasus.tools.clustering - INFO - Louvain clustering is done. Get 16 clusters.
2022-03-06 22:13:56,109 - pegasus.tools.clustering - INFO - Function 'louvain' finished in 24.09s.
2022-03-06 22:13:56,110 - pegasus.tools.nearest_neighbors - INFO - Found cached kNN results, no calculation is required.
2022-03-06 22:13:56,112 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 0.00s.
UMAP(min_dist=0.5, precomputed_knn=(array([[ 0, 32454, 33203, ..., 34421, 4585, 33415],
[ 1, 7658, 32973, ..., 33173, 21503, 20769],
[ 2, 13228, 20787, ..., 27422, 28285, 34507],
...,
[35462, 31460, 28289, ..., 11139, 21229, 20250],
[35463, 34709, 8206, ..., 26634, 1453, 17529],
[35464, 35401, 8702, ..., 13853, 32612, 24418]]), array([[ 0. , 5.1344223, 5.1892424, ..., 5.80048 , 5.834852 ,
5.835117 ],
[ 0. , 7.705061 , 8.047319 , ..., 8.791697 , 8.835941 ,
8.85679 ],
[ 0. , 4.039253 , 4.5694957, ..., 5.1763353, 5.1773987,
5.2111535],
...,
[ 0. , 6.6650157, 6.7569427, ..., 7.623494 , 7.6727667,
7.6997523],
[ 0. , 8.1451 , 9.145099 , ..., 10.019779 , 10.028571 ,
10.075575 ],
[ 0. , 5.8484592, 5.908896 , ..., 7.372008 , 7.3747406,
7.4893894]], dtype=float32), <pegasus.tools.visualization.DummyNNDescent object at 0x7ffa1ef243d0>), random_state=0, verbose=True)
Sun Mar 6 22:13:56 2022 Construct fuzzy simplicial set
Sun Mar 6 22:13:56 2022 Construct embedding
Epochs completed: 0%| 0/200 [00:00]
Sun Mar 6 22:14:24 2022 Finished embedding 2022-03-06 22:14:24,196 - pegasus.tools.visualization - INFO - Function 'umap' finished in 28.09s.
Then show UMAP plot:
pg.scatter(data_harmony, attrs=['louvain_labels', 'Channel'], basis='umap')
Another popular data integration method is integrative Non-negative Matrix Factorization (iNMF). Pegasus includes this method in nmf-torch package.
data_inmf = data.copy()
inmf_key = pg.integrative_nmf(data_inmf, batch='Channel')
Pass 1, loss=117.85902608844847.
Pass 2, loss=116.56137485319061.
Pass 3, loss=116.41518081303.
Pass 4, loss=116.38678816965961.
Pass 5, loss=116.35485455689023.
Pass 6, loss=116.3539231960626.
Converged after 6 pass(es).
Final loss=116.32222848554677.
2022-03-06 22:14:40,681 - pegasus.tools.nmf - INFO - Function 'integrative_nmf' finished in 14.72s.
Similarly as run_harmony, when finished, the calculated embedding is stored in data_inmf.obsm['X_inmf'], and integrative_nmf returns the representation key 'inmf' as variable inmf_key. In the downstream steps, you can set rep parameter to either inmf_key or 'inmf' in Pegasus functions whenever applicable.
For details on parameters of integrative_nmf other than the default setting, please see here.
Now we can perform kNN-graph-based clustering and calculate UMAP embeddings as follows:
pg.neighbors(data_inmf, rep=inmf_key)
pg.louvain(data_inmf, rep=inmf_key)
pg.umap(data_inmf, rep=inmf_key)
2022-03-06 22:14:46,394 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 5.70s.
2022-03-06 22:14:47,758 - pegasus.tools.nearest_neighbors - INFO - Function 'calculate_affinity_matrix' finished in 1.36s.
2022-03-06 22:14:48,958 - pegasus.tools.graph_operations - INFO - Function 'construct_graph' finished in 1.20s.
2022-03-06 22:15:21,712 - pegasus.tools.clustering - INFO - Louvain clustering is done. Get 21 clusters.
2022-03-06 22:15:21,751 - pegasus.tools.clustering - INFO - Function 'louvain' finished in 33.99s.
2022-03-06 22:15:21,753 - pegasus.tools.nearest_neighbors - INFO - Found cached kNN results, no calculation is required.
2022-03-06 22:15:21,755 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 0.00s.
UMAP(min_dist=0.5, precomputed_knn=(array([[ 0, 34398, 32771, ..., 23844, 34883, 19660],
[ 1, 17460, 9917, ..., 13891, 24639, 10047],
[ 2, 12901, 24726, ..., 23276, 31741, 13626],
...,
[35462, 28289, 28466, ..., 8846, 10412, 31036],
[35463, 12858, 14526, ..., 22822, 3223, 19133],
[35464, 8702, 19094, ..., 17445, 25361, 14784]]), array([[0. , 0.00393347, 0.00431629, ..., 0.00756522, 0.0076522 ,
0.00767303],
[0. , 0.00974116, 0.01254273, ..., 0.01490601, 0.01491022,
0.01492181],
[0. , 0.00640081, 0.00652797, ..., 0.00875577, 0.00877042,
0.00895062],
...,
[0. , 0.00718205, 0.00722402, ..., 0.01188747, 0.01192109,
0.01207906],
[0. , 0.01396254, 0.0143208 , ..., 0.01749072, 0.01750132,
0.01759656],
[0. , 0.01548291, 0.01767099, ..., 0.02502206, 0.02531232,
0.02534612]], dtype=float32), <pegasus.tools.visualization.DummyNNDescent object at 0x7ff8a4746890>), random_state=0, verbose=True)
Sun Mar 6 22:15:21 2022 Construct fuzzy simplicial set
Sun Mar 6 22:15:22 2022 Construct embedding
Epochs completed: 0%| 0/200 [00:00]
Sun Mar 6 22:15:48 2022 Finished embedding 2022-03-06 22:15:48,673 - pegasus.tools.visualization - INFO - Function 'umap' finished in 26.92s.
Then show UMAP plot:
pg.scatter(data_inmf, attrs=['louvain_labels', 'Channel'], basis='umap')
data_scan = data.copy()
scan_key = pg.run_scanorama(data_scan)
2022-03-06 22:15:50,782 - pegasus.tools.batch_correction - INFO - Start integration using Scanorama. Found 2000 genes among all datasets [[0. 0.69309173 0.3008284 0.50305776 0.51100244 0.46259378 0.56987542 0.39215252] [0. 0. 0.13964497 0.66464032 0.32518337 0.60141844 0.66281662 0.22722235] [0. 0. 0. 0.17857143 0.60828402 0.08767417 0.16402367 0.56236686] [0. 0. 0. 0. 0.46405868 0.63729904 0.56303931 0.42340944] [0. 0. 0. 0. 0. 0.40660147 0.49731051 0.68344048] [0. 0. 0. 0. 0. 0. 0.68938907 0.33461951] [0. 0. 0. 0. 0. 0. 0. 0.6031922 ] [0. 0. 0. 0. 0. 0. 0. 0. ]] Processing datasets (0, 1) Processing datasets (5, 6) Processing datasets (4, 7) Processing datasets (1, 3) Processing datasets (1, 6) Processing datasets (3, 5) Processing datasets (2, 4) Processing datasets (6, 7) Processing datasets (1, 5) Processing datasets (0, 6) Processing datasets (3, 6) Processing datasets (2, 7) Processing datasets (0, 4) Processing datasets (0, 3) Processing datasets (4, 6) Processing datasets (3, 4) Processing datasets (0, 5) Processing datasets (3, 7) Processing datasets (4, 5) Processing datasets (0, 7) Processing datasets (5, 7) Processing datasets (1, 4) Processing datasets (0, 2) Processing datasets (1, 7) Processing datasets (2, 3) Processing datasets (2, 6) Processing datasets (1, 2) 2022-03-06 22:17:23,134 - pegasus.tools.batch_correction - INFO - Function 'run_scanorama' finished in 92.45s.
You can check details on run_scanorama parameters here.
By default, it considers count matrix only regarding the selected HVGs, and calculates the corrected PCA matrix of $50$ PCs. When finished, this new PCA matrix is stored in data_scan.obsm['X_scanorama'], and returns its representation key 'scanorama' as variable scan_key. In the downstream steps, you can set rep parameter to either scan_key or 'scanorama' in Pegasus functions whenever applicable:
pg.neighbors(data_scan, rep=scan_key)
pg.louvain(data_scan, rep=scan_key)
pg.umap(data_scan, rep=scan_key)
2022-03-06 22:17:36,286 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 13.13s.
2022-03-06 22:17:37,600 - pegasus.tools.nearest_neighbors - INFO - Function 'calculate_affinity_matrix' finished in 1.31s.
2022-03-06 22:17:38,953 - pegasus.tools.graph_operations - INFO - Function 'construct_graph' finished in 1.35s.
2022-03-06 22:18:01,151 - pegasus.tools.clustering - INFO - Louvain clustering is done. Get 18 clusters.
2022-03-06 22:18:01,183 - pegasus.tools.clustering - INFO - Function 'louvain' finished in 23.58s.
2022-03-06 22:18:01,184 - pegasus.tools.nearest_neighbors - INFO - Found cached kNN results, no calculation is required.
2022-03-06 22:18:01,185 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 0.00s.
UMAP(min_dist=0.5, precomputed_knn=(array([[ 0, 35281, 17697, ..., 27898, 20930, 21791],
[ 1, 21594, 31342, ..., 1652, 2926, 21741],
[ 2, 22804, 18671, ..., 30580, 1164, 11422],
...,
[35462, 30699, 20801, ..., 28289, 30824, 15156],
[35463, 5765, 30996, ..., 29807, 26634, 32077],
[35464, 16222, 4418, ..., 6264, 10528, 13658]]), array([[0. , 0.17632282, 0.17862491, ..., 0.18834352, 0.19007637,
0.19024631],
[0. , 0.14160971, 0.14302711, ..., 0.15059991, 0.15074497,
0.15079884],
[0. , 0.17149583, 0.17825764, ..., 0.19416182, 0.19573236,
0.19583887],
...,
[0. , 0.14235632, 0.14390914, ..., 0.15604278, 0.15613748,
0.15681536],
[0. , 0.12512727, 0.12525763, ..., 0.14458343, 0.14480108,
0.14541759],
[0. , 0.12967643, 0.14857674, ..., 0.17032939, 0.17215477,
0.17279972]], dtype=float32), <pegasus.tools.visualization.DummyNNDescent object at 0x7ff8a6253d10>), random_state=0, verbose=True)
Sun Mar 6 22:18:01 2022 Construct fuzzy simplicial set
Sun Mar 6 22:18:01 2022 Construct embedding
Epochs completed: 0%| 0/200 [00:00]
Sun Mar 6 22:18:31 2022 Finished embedding 2022-03-06 22:18:31,257 - pegasus.tools.visualization - INFO - Function 'umap' finished in 30.07s.
Now check its UMAP plot:
pg.scatter(data_scan, attrs=['louvain_labels', 'Channel'], basis='umap')
To compare the performance on the three methods, one metric is runtime, which you can see from the logs in sections above: integrativ nmf method is the fastest, then Harmony, and Scanorama is the slowest.
In this section, we'll use 2 other metrics for comparison:
We have 4 results: No batch correction (Baseline), Harmony, iNMF, and Scanorama. For each result, kBET and kSIM acceptance rates are calculated on its 2D UMAP coordinates for comparison, which is consistent with Cumulus paper.
Details on these 2 metrics can also be found in Cumulus paper.
We can use calc_kBET function to calculate on kBET acceptance rates. Besides,
attr parameter, use the batch key, which is 'Channel' in this tutorial.rep parameter, set to the corresponding UMAP coordinates;_, _, kBET_baseline = pg.calc_kBET(data_baseline, attr='Channel', rep='umap')
_, _, kBET_harmony = pg.calc_kBET(data_harmony, attr='Channel', rep='umap')
_, _, kBET_inmf = pg.calc_kBET(data_inmf, attr='Channel', rep='umap')
_, _, kBET_scan = pg.calc_kBET(data_scan, attr='Channel', rep='umap')
2022-03-06 22:18:36,469 - pegasus.tools.nearest_neighbors - INFO - Function 'get_neighbors' finished in 3.59s.
We need pre-annotated cell type information as ground truth to calculate kSIM acceptance rate. This is achieved by:
This ground truth is stored at https://storage.googleapis.com/terra-featured-workspaces/Cumulus/cell_types.csv, or we can get it using gsutil from its Google bucket URL (gs://terra-featured-workspaces/Cumulus/cell_types.csv):
Now load this file, and attach its 'cell_types' column to the 4 resulting count matrices above:
import pandas as pd
import numpy as np
df_celltypes = pd.read_csv("cell_types.csv", index_col='barcodekey')
assert np.sum(df_celltypes.index!=data_baseline.obs_names) == 0
data_baseline.obs['cell_types'] = df_celltypes['cell_types']
assert np.sum(df_celltypes.index!=data_harmony.obs_names) == 0
data_harmony.obs['cell_types'] = df_celltypes['cell_types']
assert np.sum(df_celltypes.index!=data_inmf.obs_names) == 0
data_inmf.obs['cell_types'] = df_celltypes['cell_types']
assert np.sum(df_celltypes.index!=data_scan.obs_names) == 0
data_scan.obs['cell_types'] = df_celltypes['cell_types']
We can then use calc_kSIM function to calculate kSIM acceptance rates. Besides,
attr parameter, use the ground truth key 'cell_types';rep parameter, similarly as in kBET section, set to the corresponding UMAP coordinates;_, kSIM_baseline = pg.calc_kSIM(data_baseline, attr='cell_types', rep='umap')
_, kSIM_harmony = pg.calc_kSIM(data_harmony, attr='cell_types', rep='umap')
_, kSIM_inmf = pg.calc_kSIM(data_inmf, attr='cell_types', rep='umap')
_, kSIM_scan = pg.calc_kSIM(data_scan, attr='cell_types', rep='umap')
Now draw a scatterplot regarding these two metrics on the 4 results:
import seaborn as sns
import matplotlib.pyplot as plt
df_plot = pd.DataFrame({'method': ['Baseline', 'Harmony', 'iNMF', 'Scanorama'],
'kBET': [kBET_baseline, kBET_harmony, kBET_inmf, kBET_scan],
'kSIM': [kSIM_baseline, kSIM_harmony, kSIM_inmf, kSIM_scan]})
plt.figure(dpi=100)
ax = sns.scatterplot(x = 'kSIM', y = 'kBET', hue = 'method', data = df_plot, legend = False)
for line in range(0, df_plot.shape[0]):
x_pos = df_plot.kSIM[line] + 0.003
if df_plot.method[line] == 'Baseline':
x_pos = df_plot.kSIM[line] - 0.003
y_pos = df_plot.kBET[line]
if df_plot.method[line] == 'iNMF':
y_pos -= 0.01
alignment = 'right' if df_plot.method[line] == 'Baseline' else 'left'
ax.text(x_pos, y_pos, df_plot.method[line], ha = alignment, size = 'medium', color = 'black')
plt.xlabel('kSIM acceptance rate')
plt.ylabel('kBET acceptance rate')
As this plot shows, a trade-off exists between good mixture of cells (in terms of kBET acceptance rate) and maintaining the biology well (in terms of kSIM acceptance rate). iNMF method achieves the best mixture of cells, while its consistency with the ground truth biology is the least. Harmony and Scanorama both have a better balance between the two measurements.
Therefore, in general, the choice of batch correction method really depends on the dataset and your analysis goal.